import {
  queryVehicleList,
  deleteVehicle,
  AddVehicle,
  UpdateVehicle,
  SearchListVehicle,
  queryLocationObd,
  queryOrgMenuList,
  queryVehicleByTeam,
  queryObdDeviceList,
  queryDriverList
} from '@/services/api';

export default {
  namespace: 'vehicle',

  state: {
    obdList: [],
    searchList: [],
  },

  effects: {
    // 车辆信息
    *fetchObdList({ payload, callback }, { call, put }) {
      const response = yield call(queryVehicleList, payload);
      if (callback) callback(response);
      yield put({
        type: 'queryObdList',
        payload: response,
      });
    },
    *fetchObdRemove({ payload, callback }, { call, put }) {
      const response = yield call(deleteVehicle, payload);
      if (callback) callback(response);
    },
    *fetchObdAdd({ payload, callback }, { call, put }) {
      const response = yield call(AddVehicle, payload);
      if (callback) callback(response);
    },
    *fetchObdUpdate({ payload, callback }, { call, put }) {
      const response = yield call(UpdateVehicle, payload);
      if (callback) callback(response);
    },
    *fetchObdSearchList({ payload, callback }, { call, put }) {
      const response = yield call(SearchListVehicle, payload);
      if (callback) callback(response);
      yield put({
        type: 'searchObdList',
        payload: response,
      });
    },
    *fetchObdLocation({ payload, callback }, { call, put }) {
      const response = yield call(queryLocationObd, payload);
      if (callback) callback(response);
    },
    // 车辆监控
    *fetchQueryOrgMenuList({ payload, callback }, { call, put }) {
      const response = yield call(queryOrgMenuList, payload);
      if (callback) callback(response);
    },
    *fetchQueryVehicleByTeam({ payload, callback }, { call, put }) {
      const response = yield call(queryVehicleByTeam, payload);
      if (callback) callback(response);
    },
    // 终端设备
    *fetchQueryObdDeviceList({ payload, callback }, { call, put }) {
      const response = yield call(queryObdDeviceList, payload);
      if (callback) callback(response);
    },
    // 司机列表
    *fetchQueryDriverList({ payload, callback }, { call, put }) {
      const response = yield call(queryDriverList, payload);
      if (callback) callback(response);
    }
  },
  reducers: {
    queryObdList(state, action) {
      return {
        ...state,
        obdList: action.payload,
      };
    },
    searchObdList(state, action) {
      return {
        ...state,
        searchList: action.payload,
      };
    }
  },
};
